package com.zagj.videocomparess.utils;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;

/**
 * Created by Chen qin lang on 2018/6/21.
 */

public class AudioPlayer {
    static {
        System.loadLibrary("splaymusice");
    }
    private AudioTrack audioTrack;

    public native int playMusic(String input);
    public native int openes(String input);

    public  void setFileSize(int fileSize,int i){
        Log.e( "setFileSize: %d", fileSize+"");
        //cameraActivity.setProgressSize(fileSize);
    }
    public void createAudioTrack(int sampleInHZ,int channelCount){
        int channelConfig;
        if (channelCount==2){
            channelConfig= AudioFormat.CHANNEL_OUT_STEREO;
        }else {
            channelConfig= AudioFormat.CHANNEL_IN_MONO;
        }
        int minBufferSize = AudioTrack.getMinBufferSize(sampleInHZ, channelConfig, AudioFormat.ENCODING_PCM_16BIT);

        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,sampleInHZ,channelConfig,
                AudioFormat.ENCODING_PCM_16BIT,minBufferSize,AudioTrack.MODE_STREAM);
        audioTrack.play();
        Log.e("createAudioTrack: ", "123123");
    }
    //C传入音频数据
    public void playTrack(byte[] buffer, int lenth) {
        Log.e("playTrack: ", "123");
        if (audioTrack != null && audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
            audioTrack.write(buffer, 0, lenth);
            Log.e("createAudioTrack: ", "playTrack");
        }
    }

    public native void  stop();
}
